package com.prs.crm.action.oa;

import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.TreeNode;

import com.prs.crm.domain.oa.Archives;
import com.prs.crm.service.oa.ArchivesService;

public class ArchivesAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private TreeNode root;
	private ArchivesService archivesService;
	
	@Action(value = "loadArchivesTree", results = { @Result(name = "success", type = "json", params = {
			"root", "root" }) })
	public String loadTree() {
		root = new TreeNode();
		root.setId("-1");

		TreeNode firstNode = new TreeNode();
		firstNode.setId("0");
		firstNode.setText("档案类目");

		Collection<Archives> Archivess = this.archivesService.getRoot();

		for (Archives Archives : Archivess) {
			TreeNode node = new TreeNode();
			node.setText(Archives.getName()+"["+Archives.getCode()+"]");
			node.setId(Archives.getId().toString());
			firstNode.addChildNode(node);
			addDepartNode(node, Archives);
		}

		root.addChildNode(firstNode);
		return SUCCESS;
	}
	private void addDepartNode(TreeNode parent, Archives dept) {
		for (Archives depart : dept.getChilds()) {
			TreeNode node = new TreeNode();
			node.setText(depart.getName()+"["+depart.getCode()+"]");
			node.setId(depart.getId().toString());
			parent.addChildNode(node);
			addDepartNode(node, depart);
		}
	}

	public TreeNode getRoot() {
		return root;
	}
	public void setRoot(TreeNode root) {
		this.root = root;
	}
	public ArchivesService getArchivesService() {
		return archivesService;
	}
	public void setArchivesService(ArchivesService archivesService) {
		this.archivesService = archivesService;
	}
	
}
